//
//  Fraction.m
//  OC3_继承课后作业
//
//  Created by dllo on 15/10/26.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction

- (NSInteger)fenzi
{
    return _fenzi;
}

- (void)setFenzi:(NSInteger)fenzi
{
    _fenzi = fenzi;
}
- (NSInteger)fenmu
{
    return _fenmu;
}
- (void)setFenmu:(NSInteger)fenmu
{
    _fenmu = fenmu;
}
- (id)initWithFenzi:(NSInteger)fenzi fenmu:(NSInteger)fenmu
{
    self = [super init];
    if (self) {
        [self setFenzi:fenzi];
        [self setFenmu:fenmu];
    }
    return self;
}

+ (id)frationWithFenzi:(NSInteger)fenzi fenmu:(NSInteger)fenmu
{
    Fraction *f = [[Fraction alloc] initWithFenzi:fenzi fenmu:fenmu];
    return f;
}

// 打印
- (void)info:(Fraction *)a
{
    NSLog(@"%ld/%ld", a->_fenzi, a->_fenmu);
}

// 约分

- (void)yuefen:(Fraction *)a
{
    for (NSInteger i = a->_fenzi; i > 1; i--) {
        if (0 == a->_fenzi % i && 0 == a->_fenmu % i) {
            a->_fenzi = a->_fenzi / i;
            a->_fenmu = a->_fenmu / i;
        }
    }
}

// 加法

- (id)addFraction:(Fraction *)addFraction anotherFraction:(Fraction *)anotherFraction
{
    Fraction *c = [Fraction frationWithFenzi:0 fenmu:0];
    c->_fenzi = addFraction->_fenzi * anotherFraction->_fenmu + addFraction->_fenmu * anotherFraction->_fenzi;
    c->_fenmu = addFraction->_fenmu * anotherFraction->_fenmu;
    return c;
}

// 减法

- (id)subFraction:(Fraction *)subFraction anotherFraction:(Fraction *)anotherFraction
{
    Fraction *c = [Fraction frationWithFenzi:0 fenmu:0];
    c->_fenzi = subFraction->_fenzi * anotherFraction->_fenmu - subFraction->_fenmu * anotherFraction->_fenzi;
    c->_fenmu = subFraction->_fenmu * anotherFraction->_fenmu;
    return c;
}

// 乘法
- (id)mulFraction:(Fraction *)mulFraction anotherFraction:(Fraction *)anotherFraction
{
//    Fraction *c = [Fraction frationWithFenzi:0 fenmu:0];
    Fraction *c = nil;
    [c setFenzi:[mulFraction fenzi] * [anotherFraction fenzi]];        // 最好的写法
    _fenmu = [mulFraction fenmu] * [anotherFraction fenmu];
    return c;
}


// 除法
- (id)divFraction:(Fraction *)divFraction anotherFraction:(Fraction *)anotherFraction
{
    Fraction *c = [Fraction frationWithFenzi:0 fenmu:0];
    c.fenzi = divFraction->_fenzi * anotherFraction->_fenmu;         // 点语法
    c->_fenmu = divFraction->_fenmu * anotherFraction->_fenzi;
    return c;
}





@end
